package com.dianping.cat.home.dependency.format.transform;

import com.dianping.cat.home.dependency.format.IEntity;
import com.dianping.cat.home.dependency.format.IVisitor;
import com.dianping.cat.home.dependency.format.entity.Domain;
import com.dianping.cat.home.dependency.format.entity.ProductLine;
import com.dianping.cat.home.dependency.format.entity.TopoGraphFormatConfig;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;










public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

  public DefaultXmlBuilder() { this(false); }



/*  33 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  37 */     this.m_compact = compact;
/*  38 */     this.m_sb = sb;
/*  39 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  43 */     entity.accept(this.m_visitor);
/*  44 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  48 */     this.m_level--;

/*  50 */     indent();
/*  51 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  55 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  59 */     if (value == null) {
/*  60 */       return null;
    }

/*  63 */     String str = toString(value);
/*  64 */     int len = str.length();
/*  65 */     StringBuilder sb = new StringBuilder(len + 16);

/*  67 */     for (int i = 0; i < len; i++) {
/*  68 */       char ch = str.charAt(i);

/*  70 */       switch (ch) {
        case '<':
/*  72 */           sb.append("&lt;");
          break;
        case '>':
/*  75 */           sb.append("&gt;");
          break;
        case '&':
          sb.append("&amp;");
          break;
        case '"':
/*  81 */           if (!text) {
/*  82 */             sb.append("&quot;");
            break;
          }
        default:
/*  86 */           sb.append(ch);
          break;
      }

    }
/*  91 */     return sb.toString();
  }

  protected void indent() {
    if (!this.m_compact) {
/*  96 */       for (int i = this.m_level - 1; i >= 0; i--) {
/*  97 */         this.m_sb.append("   ");
      }
    }
  }


/* 103 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 107 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 111 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 115 */     indent();

/* 117 */     this.m_sb.append('<').append(name);

/* 119 */     int len = nameValues.length;

/* 121 */     for (int i = 0; i + 1 < len; i += 2) {
/* 122 */       Object attrName = nameValues[i];
/* 123 */       Object attrValue = nameValues[i + 1];

/* 125 */       if (attrValue != null) {
/* 126 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 130 */     if (dynamicAttributes != null) {
/* 131 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 132 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 136 */     if (text != null && closed) {
/* 137 */       this.m_sb.append('>');
/* 138 */       this.m_sb.append(escape(text, true));
/* 139 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 141 */       if (closed) {
/* 142 */         this.m_sb.append('/');
      } else {
/* 144 */         this.m_level++;
      }

/* 147 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 153 */     if (value instanceof String)
/* 154 */       return (String)value; 
/* 155 */     if (value instanceof Collection) {
/* 156 */       Collection<Object> list = (Collection)value;
/* 157 */       StringBuilder sb = new StringBuilder(32);
/* 158 */       boolean first = true;

/* 160 */       for (Object item : list) {
/* 161 */         if (first) {
/* 162 */           first = false;
        } else {
/* 164 */           sb.append(',');
        }

/* 167 */         if (item != null) {
/* 168 */           sb.append(item);
        }
      }

/* 172 */       return sb.toString();
/* 173 */     }  if (value.getClass().isArray()) {
/* 174 */       int len = Array.getLength(value);
/* 175 */       StringBuilder sb = new StringBuilder(32);
/* 176 */       boolean first = true;

/* 178 */       for (int i = 0; i < len; i++) {
/* 179 */         Object item = Array.get(value, i);

/* 181 */         if (first) {
/* 182 */           first = false;
        } else {
/* 184 */           sb.append(',');
        }

/* 187 */         if (item != null) {
/* 188 */           sb.append(item);
        }
      }

/* 192 */       return sb.toString();
    }

/* 195 */     return String.valueOf(value);
  }



/* 200 */   public void visitDomain(Domain domain) { startTag("domain", true, null, new Object[] { "id", domain.getId(), "colInside", domain.getColInside() }); }



  public void visitProductLine(ProductLine productLine) {
/* 205 */     startTag("productLine", null, new Object[] { "id", productLine.getId(), "colInside", productLine.getColInside() });

/* 207 */     if (!productLine.getDomains().isEmpty()) {
/* 208 */       for (Domain domain : productLine.getDomains()) {
/* 209 */         domain.accept(this.m_visitor);
      }
    }

/* 213 */     endTag("productLine");
  }


  public void visitTopoGraphFormatConfig(TopoGraphFormatConfig topoGraphFormatConfig) {
/* 218 */     startTag("topoGraph-format-config", null, new Object[0]);

/* 220 */     if (!topoGraphFormatConfig.getProductLines().isEmpty()) {
/* 221 */       for (ProductLine productLine : topoGraphFormatConfig.getProductLines()) {
/* 222 */         productLine.accept(this.m_visitor);
      }
    }

/* 226 */     endTag("topoGraph-format-config");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/dependency/format/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */